package Utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.jdbc.SuspendableXAConnection;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class GetStockInfo
{
    //A股接口URL
    private static String AGU_URL = "http://127.0.0.1:8080/api/public/stock_info_a_code_name";
    //股票信息URL
    private static String StockInfo_URL = "http://127.0.0.1:8080/api/public/stock_zh_a_spot_em";
    //股票历史信息URL
    private static String StockHistoryInfo_URL = "http://127.0.0.1:8080/api/public/stock_zh_a_hist";
    public static  ArrayList<Stock> getStock(){
        //创建结果列表
        ArrayList<Stock> StockList = new ArrayList<Stock>();
        //参数列表
        Map<String,String> params = null;
        //获取股票数据
        String ResultString = RestTemplateUtils.doGet(AGU_URL,params);
        ObjectMapper objectMapper = new ObjectMapper();
        try
        {
            ArrayList<LinkedHashMap> APIReturn = objectMapper.readValue(ResultString,ArrayList.class);
            //将股票数据构建Stock对象
            for(LinkedHashMap lkh : APIReturn)
            {
                String StockCode = (String) lkh.get("code");
                String StockName = (String) lkh.get("name");
                StockList.add(new Stock(StockCode,StockName));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return StockList;
    }
    public static ArrayList<StockInfo> getStockInfo()
    {
        //创建结果列表
        ArrayList<StockInfo> StockInfoList = new ArrayList<>();
        //参数列表
        Map<String,String> params = null;
        //获取数据
        String ResultString = RestTemplateUtils.doGet(StockInfo_URL,params);
        ObjectMapper objectMapper = new ObjectMapper();
        try
        {
            ArrayList<LinkedHashMap> APIReturn = objectMapper.readValue(ResultString,ArrayList.class);
            //将股票数据构建Stock对象
            for(LinkedHashMap lkh : APIReturn)
            {
                String Code = (String) lkh.get("代码");
                double LatestPrice = lkh.get("最新价") == null ? 0 : (double) lkh.get("最新价");
                double AOIAAOD = lkh.get("涨跌幅") == null ? 0 : (double) lkh.get("涨跌幅");
                double IAD = lkh.get("涨跌额") == null ? 0 : (double) lkh.get("涨跌额");
                double TradingVolume = lkh.get("成交量") == null ? 0 : (double) lkh.get("成交量");
                double BusinessVolume = lkh.get("成交额") == null ? 0 : (double) lkh.get("成交额");
                double Amplitude = lkh.get("振幅") == null ? 0 : (double) lkh.get("振幅");
                double HighestPrice = lkh.get("最高") == null ? 0 : (double) lkh.get("最高");
                double LowestPrice = lkh.get("最低") == null ? 0 : (double) lkh.get("最低");
                double TodayOpen = lkh.get("今开") == null ? 0 : (double) lkh.get("今开");
                double YesterDayClose = lkh.get("昨收") == null ? 0 : (double) lkh.get("昨收");
                double VolumeRatio = lkh.get("量比") == null ? 0 : (double) lkh.get("量比");
                double TurnoverRatio = lkh.get("换手率") == null ? 0 : (double) lkh.get("换手率");
                double PERatioDymatic = lkh.get("市盈率-动态") == null ? 0 : (double) lkh.get("市盈率-动态");
                double PBRatio = lkh.get("市净率") == null ? 0 : (double) lkh.get("市净率");
                double MarketCap = lkh.get("总市值") == null ? 0 : (double) lkh.get("总市值");
                double CirculationMarketValue = lkh.get("流通市值") == null ? 0 : (double) lkh.get("流通市值");
                double RisingSpeed = lkh.get("涨速") == null ? 0 : (double) lkh.get("涨速");
                double UpAndDown5M = lkh.get("5分钟涨跌") == null ? 0 : (double) lkh.get("5分钟涨跌");
                double UpAndDown60D =lkh.get("60日涨跌幅") == null ? 0 : (double) lkh.get("60日涨跌幅");
                double UpAndDownUN = lkh.get("年初至今涨跌幅") == null ? 0 : (double) lkh.get("年初至今涨跌幅");

                //加入列表
                StockInfoList.add(new StockInfo(Code,LatestPrice,AOIAAOD,IAD,TradingVolume,BusinessVolume,Amplitude,HighestPrice,LowestPrice,TodayOpen,YesterDayClose,
                        VolumeRatio,TurnoverRatio,PERatioDymatic,PBRatio,MarketCap,CirculationMarketValue,RisingSpeed,UpAndDown5M,UpAndDown60D,UpAndDownUN));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return StockInfoList;
    }
    //获取距今30天股票历史信息
    public static ArrayList<StockHistoryInfo> GetHistoryStockInfo(String StockCode)
    {
        //创建结果列表
        ArrayList<StockHistoryInfo> StockHistoryInfoList = new ArrayList<>();
        //参数列表
        Map<String,String> params = new HashMap<>();
        //添加参数
        params.put("symbol",StockCode);
        params.put("period","daily");
        LocalDateTime now = LocalDateTime.now();
        String endtime = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String starttime = now.minus(30, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        params.put("start_date",starttime);
        params.put("end_date",endtime);
        //获取数据
        String ResultString = RestTemplateUtils.doGet(StockHistoryInfo_URL,params);
        System.out.println(ResultString);
        ObjectMapper objectMapper = new ObjectMapper();
        try
        {
            ArrayList<LinkedHashMap> APIReturn = objectMapper.readValue(ResultString,ArrayList.class);
            //将股票数据构建Stock对象
            for(LinkedHashMap lkh : APIReturn)
            {
                String date = (String) lkh.get("日期");
                String Date = date.toString();
                double Open = lkh.get("开盘") == null ? 0 : (double) lkh.get("开盘");
                double Close = lkh.get("收盘") == null ? 0 : (double) lkh.get("收盘");
                double Highest = lkh.get("最高") == null ? 0 : (double) lkh.get("最高");
                double Lowest = lkh.get("最低") == null ? 0 : (double) lkh.get("最低");
                int BusinessVolume = lkh.get("成交量") == null ? 0 : (int) lkh.get("成交量");
                //加入列表
                StockHistoryInfoList.add(new StockHistoryInfo(Date,Open,Close,Highest,Lowest,BusinessVolume));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return StockHistoryInfoList;
    }
}
